let OSS = require('ali-oss')

let aliOss = new OSS({
  region: 'oss-cn-beijing',
  accessKeyId: 'LTAI5tG8fVAQBq3Ha1uhvbZn',
  accessKeySecret: 'hEUEmPwuWydtL4iB2JgWh1YO6cmdEy',
  bucket: 'hixi-show'
  // bucket: process.env.NODE_ENV == 'development' ? 'hixicasting-test' : 'hixicasting'
})
const headers = {
  'x-oss-forbid-overwrite': true
}

export const put = async (ObjName, fileUrl) => {
  try {
    let result = await aliOss.put(`${ObjName}`, fileUrl)
    return result
  } catch (e) {
    console.log(e)
  }
}

export const getFile = async ObjName => {
  try {
    let result = await aliOss.signatureUrl(`${ObjName}`)
    return result
  } catch (e) {
    console.log(e)
  }
}

export const getFileNameUUID = () => {
  function rx() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return `${+new Date()}_${rx()}${rx()}`
}

// 获取后缀
export const getFileName = fileName => {
  const lastDotIndex = fileName.lastIndexOf('.')
  return lastDotIndex === -1 ? '' : fileName.substring(lastDotIndex)
}

// 判断是否为视频
export const isVideo = name => {
  const videoExtensions = ['.mp4', '.webm', '.ogg', '.mpeg', '.mov', '.avi', '.wmv', '.flv', '.mkv']
  const fileName = name.toLowerCase()
  return videoExtensions.some(extension => fileName.endsWith(extension))
}
